﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class customerAction : System.Web.UI.Page
{
    private static readonly ILog logger = LogManager.GetLogger(typeof(customerAction).Name);
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                object customerId = this.Context.Items["id"];
                string temp = String.Empty;
                if (customerId != null)
                    temp = customerId.ToString();
                Inits(temp);
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// InsertOrUpdate :when press [Them/sua] button
    /// </summary>
    private void InsertOrUpdate()
    {
        if (Session["Employee"] == null)
        {
            return;
        }
        Guid employeeId = Guid.Empty;
        Employee emp= (Employee)Session["Employee"];
        employeeId = emp.Id;
        //get id from hidden field
        string id = hdnId.Value;

        //init BO
        Customer customer = new Customer();
        customer.CustomerCode = txtCustomerCode.Text.Trim();
        customer.CustomerName = txtName.Text.Trim();
        customer.Address = txtAddress.Text.Trim();
        customer.Phone = txtPhone.Text.Trim();
        customer.LastUser = emp;
        if (!string.IsNullOrEmpty(hdnId.Value))
        {
            customer.CustomerID = new Guid(id);
        }

        try
        {
            if (!IsExistedCode)
            {
                customer.Save(customer);
                Response.Redirect(Constants.CUSTOMER_SHOW+"?page=3");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private bool IsExistedCode
    {
        get
        {
            string code = txtCustomerCode.Text.Trim();
            if (code!=hdnCode.Value && new Customer().IsExistedCustomerCode(code))
            {
                lblErrorCode.Visible = true;
                txtCustomerCode.Focus();
            }
            else
            {
                lblErrorCode.Visible = false;
            }
            return lblErrorCode.Visible;
        }
        
    }


    /// <summary>
    /// Init page
    /// </summary>
    /// <param name="id"></param>
    private void Inits(string id)
    {        
        //handle [Xoa][Huy] button
        btnReset.ImageUrl = "images/xoa.gif";
        btnReset.Attributes.Add("onmouseover", "this.src='images/xoa_hover.gif'");
        btnReset.Attributes.Add("onmouseout", "this.src='images/xoa.gif'");
        btnCancel.ImageUrl = "images/huy.gif";
        btnCancel.Attributes.Add("onmouseover", "this.src='images/huy_hover.gif'");
        btnCancel.Attributes.Add("onmouseout", "this.src='images/huy.gif'");

        //Insert
        if (String.IsNullOrEmpty(id))
        {
            //handle [Save] button
            btnSave.ImageUrl = "images/save.gif";
            btnSave.Attributes.Add("onmouseover", "this.src='images/save_hover.gif'");
            btnSave.Attributes.Add("onmouseout", "this.src='images/save.gif'");

            //handle title
            lblTitle.Text = "Thêm khách hàng";

            //Generate empCode
            if (!Page.IsPostBack)
            {
                txtCustomerCode.Text = new Customer().GetMaxCusCode();
            }                

        }//Update
        else
        {
            //handle [Save] button
            btnSave.ImageUrl = "images/edit.jpg";
            btnSave.Attributes.Add("onmouseover", "this.src='images/edit_hover.jpg'");
            btnSave.Attributes.Add("onmouseout", "this.src='images/edit.jpg'");

            //handle title
            lblTitle.Text = "Chỉnh sửa thông tin khách hàng";

            ////disable code and username
            //txtUser.ReadOnly = true;
            //txtUser.CssClass = "disable width_200";

            //load to controls
            LoadUpdate(new Guid(id));
        }

        //page title
        Page.Title = Constants.TITLE_SYSTEM + Constants.SPACE + Constants.SEPARATE + Constants.SPACE + lblTitle.Text;

        //data to test
        // TestData();
    }

    /// <summary>
    /// LoadUpdate
    /// </summary>
    /// <param name="id"></param>
    private void LoadUpdate(Guid id)
    {
        try
        {
            Customer cus = new Customer();
            cus = cus.GetById(id);
            txtCustomerCode.Text = cus.CustomerCode;
            hdnCode.Value = txtCustomerCode.Text;
            txtName.Text = cus.CustomerName;
            txtPhone.Text = cus.Phone;
            txtAddress.Text = cus.Address;
            hdnId.Value = id.ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    
    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect(Constants.CUSTOMER_SHOW+"?page=3", false);
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        try
        {            
            InsertOrUpdate();            
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }
    protected void btnReset_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(hdnId.Value))
            {
                txtCustomerCode.Text = String.Empty;
                txtName.Text = String.Empty;
                txtPhone.Text = String.Empty;
                txtAddress.Text = String.Empty;
            }
            else
            {
                Inits(hdnId.Value);
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }
}